﻿namespace UserManger
{
    using Common;
    using Data;
    using System;
    using System.Collections;
    using System.Data;
    using System.Configuration;
    using System.Linq;
    using System.Xml.Linq;

    /// <summary>   
    ///UserManager 的摘要说明
    /// </summary>
    public class UserManager
    {
        IUser myitem;
        public UserManager()
        {
            myitem = new IUser();
        }
        public int CreateUser(User UserInfo)
        {
            User user;
            if (UserInfo.UserId == null)
                return 0;
            user = myitem.GetUserByUserId(UserInfo.UserId);
            if (user != null)
                return 0;
            else
                return myitem.Insert(UserInfo);
        }
        public bool DeleteUser(string UserId, string Code)
        {
            User user;
            if (UserId == null)
                return false;
            user = myitem.GetUserByUserId(UserId);
            if (user != null)
            {
                if (Code == user.Code)
                {
                    return myitem.Delete(UserId);
                }
                else
                    return false;
            }
            return false;
        }
        public ArrayList GetAllUsers()
        {
            return myitem.GetAllUsers();
        }
        public string GetCodeSecurityQuestion(string UserId)
        {
            User user;
            if (UserId == null)
                return null;
            user = myitem.GetUserByUserId(UserId);
            if (user != null)
            {
                return user.CodeSecurityQuestion;
            }
            return null;
        }
        public User GetUserByUserId(string UserId)
        {
            if (UserId == null)
                return null;
            return myitem.GetUserByUserId(UserId);
        }
        public int UpdateUser(User UserInfo)
        {
            User user;
            if (UserInfo.UserId == null)
                return 0;
            user = myitem.GetUserByUserId(UserInfo.UserId);
            if (user != null)
            {
                bool flag;
                flag = myitem.Update(UserInfo);
                if (flag)
                    return 1;
                else
                    return 0;
            }
            else
                return 0;
        }
        public string UserGetCode(string UserId, string CodeSecurityAnswerByUser )
        {
            User user;
            if (UserId == null)
                return null;
            user = myitem.GetUserByUserId(UserId);
            if (user != null)
            {
                user.CodeSecurityQuestion = this.GetCodeSecurityQuestion(UserId);
                if (user.CodeSecurityAnswer == CodeSecurityAnswerByUser)
                {
                    return user.Code;
                }
                else
                    return null;
            }
            return null;
        }
        public bool UserLogin(string UserId, string Code)
        {
            User user;
            if (UserId == null)
                return false;
            user = myitem.GetUserByUserId(UserId);
            if (user != null)
            {
                if (Code == user.Code)
                    return true;
                else
                    return false;
            }
            return false;
        }
    }
}
